package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.SecurityGroupDao;
import com.bocloud.cmp.entity.resource.SecurityGroup;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 安全组DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("securityGroupDao")
public class SecurityGroupDaoImpl extends BasicDao implements SecurityGroupDao {

	@Override
	public List<SecurityGroup> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select * from security_group where 1=1";
		sql = SQLHelper.buildSql(sql, page, rows, params, sorter, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, SecurityGroup.class);
		List<SecurityGroup> securityGroups = new ArrayList<>();
		SecurityGroup securityGroup = null;
		for (Object obj : list) {
			securityGroup = (SecurityGroup) obj;
			securityGroups.add(securityGroup);
		}
		return securityGroups;
	}

	@Override
	public List<SecurityGroup> listByVendorId(Long vendorId) throws Exception {
		String sql = "select * from security_group where vendor_id =:vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		List<Object> list = this.queryForList(sql, params, SecurityGroup.class);
		List<SecurityGroup> securityGroups = new ArrayList<>();
		SecurityGroup securityGroup = null;
		for (Object obj : list) {
			securityGroup = (SecurityGroup) obj;
			securityGroups.add(securityGroup);
		}
		return securityGroups;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from security_group where 1=1";
		sql = SQLHelper.buildSql(sql, params, null, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public SecurityGroup save(SecurityGroup securityGroup) throws Exception {
		return (SecurityGroup) this.saveEntity(securityGroup);
	}

	@Override
	public boolean modify(SecurityGroup securityGroup) throws Exception {
		return this.updateEntity(securityGroup);
	}

	@Override
	public boolean delete(Long id) throws Exception {
		String sql = "delete from security_group where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.update(sql, params) > 0;
	}

	@Override
	public SecurityGroup query(Long id) throws Exception {
		String sql = "select * from security_group where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Object> list = this.queryForList(sql, params, SecurityGroup.class);
		if (list.isEmpty()) {
			return null;
		}
		return (SecurityGroup) list.get(0);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name,a.uuid from security_group a where 1=1";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, SecurityGroup.class);
		List<SimpleBean> beans = new ArrayList<>();
		SecurityGroup securityGroup = null;
		for (Object obj : list) {
			securityGroup = (SecurityGroup) obj;
			beans.add(new SimpleBean(securityGroup.getId(), securityGroup.getName(), securityGroup.getUuid()));
		}
		return beans;
	}

	@Override
	public boolean removeByVid(Long vendorId) throws Exception {
		String sql = "delete from security_group where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.update(sql, params) > 0;
	}

}
